<?php
namespace ac\controllers;

use Yii;
use ac\controllers\BasicController;
use common\component\exception\ErrMap;
use common\models\OperationLog;
use common\models\Album;
use common\models\AlbumCategory;
use common\component\tool\Tool;
use common\behaviors\P3p;
use app\models\Company;


class AuthController extends BasicController
{
    protected $userInfo;//登录信息
    protected $permissions;//角色权限信息
    protected $permissionApps;//角色权限信息

    public function behaviors()
    {
        return [
             // 注册匿名行为，只有行为类名
            P3p::className(),

        ];
    }

    public function init()
    {
        parent::init();
        if(isset(Yii::$app->session->get('loginInfo')->userInfo)){
            $this->userInfo = Yii::$app->session->get('loginInfo')->userInfo;
            $this->permissions = Yii::$app->session->get('loginInfo')->permissions;
            $this->permissionApps = Yii::$app->session->get('loginInfo')->permissionApps;
        };
    }

    /*验证是否登录*/
    public function beforeAction($action)
    {
        $module = $this->module->id;
        $controller = $this->id;
        $action = $this->action->id;
        $router = $module . '=' . $controller . '=' . $action;

        // P(Yii::$app->session);
        if (parent::beforeAction($action)) {
            if(!empty($this->userInfo)){
                // echo $module . '-' . $controller . '-' . $action;
                // P($this->permissions);
                // P($this->userInfo);

                /*验证权限*/
                $pass = false;
                if($this->userInfo['issuper'] == 1 or
                    in_array($router, Yii::$app->params['allowRouter'])){//菜单列表
                    $pass = true;
                }else{
                    foreach($this->permissions as $k => $v){
                        if(isset($this->get['appid'])){
                            if($v['appid'] == $this->get['appid'] and $v['m'] == $module and $v['c'] == $controller and $v['a'] == $action){
                                $pass = true;
                                break;
                            }
                        }else{
                            // echo $v['m'] . '*' . $v['c'] . '*' . $v['a'] . '<br>';
                            // echo $module . '-' . $controller . '-' . $action . '<br>';
                            // echo '----------------<br>';
                            if($v['m'] == $module and $v['c'] == $controller and $v['a'] == $action){
                                $pass = true;
                                break;
                            }
                        }
                    }
                }
                if($pass){
                    return true;
                }else{
                    list($errno, $errmsg) = ErrMap::getError(10020);//没有权限
                }
            }
            if(!isset($errno)){
                list($errno, $errmsg) = ErrMap::getError(10007);
            }
            $args = array(
                'loginid' => 0,
                'loginname' => '',
                'back_code' => $errno,
                'back_info' => $errmsg,
            );
            if(gettype($res = OperationLog::addLog($args)) != 'boolean'){//操作日志
                Yii::$app->end($res);
            };
        }
	}


    /*
    操作日志通用参数
    $backInfo['返回码', '返回信息']
    */
    protected function operationLogArgs($backInfo = ['', ''])
    {
        $args = array(
            'loginid' => $this->userInfo['admin_id'],
            'loginname' => $this->userInfo['username'],
            'comid' => $this->userInfo['comid'],
            'back_code' => $backInfo[0],
            'back_info' => $backInfo[1]
        );
        return $args;
    }

    /*设置列表页 每页显示多少条*/
    public function actionSetPageSize(){
        if(isset($this->get['pageSize'])){
            $pageSize = $this->get['pageSize'];
            $this->setPageSize($pageSize);
            return Tool::showRes([0, '设置成功']);
        }
    }


    /*验证comid，是否在登录账号所管理的范围内*/
    protected function checkComID($comid)
    {
        if($this->userInfo['comid'] == '00000000' or $this->userInfo['comid'] == $comid){
            return $comid;
        };
        $sql = 'select comid from {{%company}} where comid = :comid and recommend = (
            select com_id from {{%company}} where comid = "'. $this->userInfo['comid'] .'"
            )';
        $company = Company::findBySql($sql, [":comid" => $comid])->one();
        if(!is_null($company)){
            return $company->comid;
        }
        return false;
    }
    

    
}
